package com.uyaer.utils
{
	import flash.utils.Dictionary;
	
	public class HashMap
	{
		private var map:Dictionary;
		private var keys:Array;
		private var weakKeys:Boolean;
		public function HashMap(weakKeys:Boolean = false)
		{
			this.weakKeys = weakKeys;
			init();
		}
		
		private function init():void{
			map = new Dictionary(weakKeys);
			keys = new Array();
		}
		
		public function getKeys():Array{
			return keys;
		}
		
		public function getValues():Array{
			var arr:Array = new Array();
			for(var key:Object in keys){
				arr.push(map[key]);
			}
			return arr;
		}
		
		public function put(key:*,value:*):void{
			map[key] = value;
			for(var i:int = 0 ; i < keys.length ; i ++){
				if(keys[i] == key){
					return;
				}
			}
			keys.push(key);
		}
		
		/**
		 *两个map联合，后者不能覆盖本身的map 
		 * @param m
		 * 
		 */             
		public function putAll(m:HashMap):void{
			var mkeys:Array = m.getKeys();
			for each (var key:Object in mkeys){
				if(!hasKey(key)){
					put(key,m.get(key));
				}       
			}
		}
		
		public function get(key:*):*{
			return map[key];
		}
		
		public function remove(key:*):void{
			if(hasKey(key)){
				map[key] = undefined;
				delete map[key];
			}
			for(var i:int = 0 ; i < keys.length ; i ++){
				if(keys[i] == key){
					keys.splice(i,1);
					return;
				}
			}
		}
		
		public function removeAll():void{
			clear();
		}
		
		public function clear():void{
			keys.length = 0;
			init();
		}
		
		public function hasKey(key:*):Boolean{
			if(map[key]){
				return true;
			}else{
				return false;
			}
		}
	}
}